from django.shortcuts import render,redirect

# Create your views here.
from dbadmin import models
import subprocess



def p_add(request):
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    if request.method == "GET":
        user_status = models.users.objects.filter(user_name=my_name).values("user_admin")[0]["user_admin"]
        if user_status == "0":
            projects_list = models.users.objects.filter(user_name=my_name).values("user_project__id","user_project__p_name","user_project__p_name_alias","user_project__p_salt__s_name")
        else:
            projects_list = models.projects.objects.values("id","p_name","p_name_alias","p_salt__s_name")
        salt_list = models.saltconfig.objects.values("id", "s_name")
        info["projects_list"] = projects_list
        print(projects_list)
        info["salt_list"] = salt_list

        return render(request,"project_add.html",info)
    else:
        p_name = request.POST.get("p_name")
        p_name_alias = request.POST.get("p_name_alias")
        p_name_status = models.projects.objects.filter(p_name_alias=p_name_alias).count()
        p_salt = request.POST.get("p_salt")
        p_cdn =  request.POST.get("p_cdn")
        p_cdn_dir = request.POST.get("p_cdn_dir")
        if p_name_status == 0 :
            models.projects.objects.create(p_name=p_name,p_name_alias=p_name_alias,p_cdn=p_cdn,p_cdn_dir=p_cdn_dir)
        else:
            pass
        id = models.projects.objects.filter(p_name_alias=p_name_alias).values("id")[0]["id"]
        obj = models.projects.objects.get(id=id)
        obj.p_salt.set([p_salt])

        return redirect("/projects/add/")
def p_del(request):
    id = request.GET.get("id")
    models.projects.objects.filter(id=id).delete()
    return redirect("/projects/add/")





def p_operation(request):
    id = request.GET.get("id")
    info = {}
    my_name = request.session.get('username')
    info["my_name"] = my_name
    project_salt_ip = models.projects.objects.filter(id=id).values("p_salt__s_ip")[0]["p_salt__s_ip"]
    cmd_list = models.saltconfig.objects.filter(s_ip=project_salt_ip).values("s_cmd__cmd_command", "s_cmd__id")
    cmd_list_script = models.saltconfig.objects.filter(s_ip=project_salt_ip, s_cmd__cmd_type="1").values(
        "s_cmd__cmd_command", "s_cmd__id")
    cmd_list_scripts = models.saltconfig.objects.filter(s_ip=project_salt_ip, s_cmd__cmd_type="3").values(
        "s_cmd__cmd_command", "s_cmd__id")
    project_name = models.projects.objects.filter(id=id).values("p_name")[0]
    salt_ip = models.projects.objects.filter(id=id).values("p_salt__s_ip")[0]["p_salt__s_ip"]
    info["cmd_list_script"] = cmd_list_script
    info["project_name"] = project_name["p_name"]
    info["project_cmd"] = cmd_list
    info["cmd_list_scripts"] = cmd_list_scripts
    if request.method == "GET":
        return render(request,"project_operation.html",info)
    else:
        fun_num = request.POST.get("fun_num")
        cmd_id = request.POST.get("cmd")
        if fun_num == "1":

            cmd_info = models.cmdlist.objects.filter(id=cmd_id).values()[0]
            if cmd_info["cmd_type"] == "1":
                exec_result = subprocess.Popen('ssh %s \'salt "%s*" cmd.run "%s"\'' %(salt_ip,project_name["p_name"],cmd_info["cmd_command"]), stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)

                info["exec_result"] = exec_result.stdout.read().decode()
                print(info["exec_result"])
                return render(request,"project_operation.html",info)
            elif cmd_info["cmd_type"] == "2":
                print(project_name)
                exec_result = subprocess.Popen(
                    'ssh %s \'salt "%s*" state.sls "%s"\'' % (salt_ip, project_name["p_name"], cmd_info["cmd_command"]),
                    stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)

                info["exec_result"] = exec_result.stdout.read().decode()
                return render(request, "project_operation.html", info)
        elif fun_num == "2":
            cmd = request.POST.get("cmd")
            cmd_command = request.POST.get("cmd_command")
            if cmd == "1":

                cmd_tmp = "ssh %s \"salt '%s*' cmd.run '%s'\" " %(salt_ip,project_name["p_name"],cmd_command)
                exec_result = subprocess.Popen(cmd_tmp , stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
                info["exec_result2"] = exec_result.stdout.read().decode()
                return render(request,"project_operation.html",info)
            elif cmd == "2":

                exec_result = subprocess.Popen(
                    "ssh %s \"salt '%s*' state.sls '%s'\"" % (salt_ip, project_name, cmd_command),
                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True)

                info["exec_result2"] = exec_result.stdout.read().decode()
                return render(request, "project_operation.html", info)
            elif cmd == "3":
                print(salt_ip,cmd_command)
                exec_result = subprocess.Popen(
                    "ssh %s \"%s\"" % (salt_ip, cmd_command),
                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True)
                info["exec_result2"] = exec_result.stdout.read().decode()
                return render(request, "project_operation.html", info)
        elif fun_num == "3":
            if request.method == "GET":
                pass
            else:
                file_update = request.POST.get("file_update")
                file_md5 = request.POST.get("file_md5")
                cmd_command = request.POST.get("cmd")
                cmd_tmp = "ssh %s \"salt '%s*' cmd.run '%s %s %s'\" " % (salt_ip, project_name["p_name"], cmd_command,file_update,file_md5)
                # print(cmd_tmp)
                exec_result = subprocess.Popen(cmd_tmp, stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
                info["exec_result3"] = exec_result.stdout.read().decode()
                return render(request, "project_operation.html", info)
        elif fun_num == "4":
            if request.method == "GET":
                pass
            else:
                file_update = request.POST.get("file_update")
                file_md5 = request.POST.get("file_md5")
                cmd_command = request.POST.get("cmd")
                cdn_info = models.projects.objects.filter(id=id).values("p_cdn","p_cdn_dir")[0]
                print(cdn_info)
                cmd_tmp = "ssh %s \"%s %s %s %s %s \" " % (salt_ip,cmd_command,file_update,file_md5,cdn_info["p_cdn"],cdn_info["p_cdn_dir"])
                # print(cmd_tmp)
                exec_result = subprocess.Popen(cmd_tmp, stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
                print("------------------------")
                info["exec_result4"] = exec_result.stdout.read().decode()

                return render(request, "project_operation.html", info)


